package com.eastcom.providor.service.impl;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.DataAbnormal;
import com.eastcom.bm.atom.ListRange;
import com.eastcom.client.DataAbnormalBean;
import com.eastcom.common.util.CharliDate;
import com.eastcom.providor.dao.IDataAbnormalDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IDataAbnormalService;
import com.eastcom.providor.service.entry.DataAbnormalEntry;
import com.eastcom.providor.service.extview.DataAbnormalUtil;

public class DataAbnormalService extends BaseService implements IDataAbnormalService{

	private static Format format = new SimpleDateFormat("MM-dd");

	private IDataAbnormalDao dataAbnormalDao;

	//private IDataSetDao dataSetDao;
	
	
	public void setDataAbnormalDao(IDataAbnormalDao dataAbnormalDao) {
		this.dataAbnormalDao = dataAbnormalDao;
	}
	
	public Map<String, Object> getMainDataAbnormal() {
		try {
			Map<String, Object> ret=new HashMap<String, Object>();
			Calendar calendar = Calendar.getInstance();
			Date end = CharliDate.getStartTimeOfCurrentDay(new Date());
			Date start = CharliDate.getApartMonthCurrentDay(end, -1);
			int numDay = CharliDate.getApartDay(start, end);
			
			List<DataAbnormal> list = dataAbnormalDao.getAllDataAbnormals(
					start, end);
			Map<String,Atom_DatasetDefine> datasets=null;//dataSetDao.getAllDataSetDefineMap();

			DataAbnormalEntry en=new DataAbnormalEntry();
			for (DataAbnormal da : list) {
				en.setValue(format.format(da.getTimeStamp()), da.getDatasetId(), da);
			}
			
			List<Atom_DatasetDefine> ats=new ArrayList<Atom_DatasetDefine>(datasets.values());
			//result �� ColNames
			List<String> cols=new ArrayList<String>();
			for(Atom_DatasetDefine at:ats){
				cols.add(at.getCn_name());
			}
			//result: times
			List<Date> dates=new ArrayList<Date>();
			calendar.setTime(start);
			int vcolName=0;
			int vtime=0;
			//result : values
			Map[][] values=new Map[ats.size()][numDay];
			for(int i=0;i<numDay;i++){
				dates.add(calendar.getTime());
				for(Atom_DatasetDefine at:ats){
					
					values[vcolName][vtime]=DataAbnormalUtil.createTableNodeByDataAbnormal(en.getValue(format.format(calendar.getTime()), at.getId()));
					//System.out.print( cols.get(vcolName)+" : "+format.format(dates.get(vtime))+"  v:"+values[vcolName][vtime]+"    ");
					vcolName++;
				}
				System.out.println();
				
				vcolName=0;
				vtime++;
				calendar.add(Calendar.DATE, 1);
			}
			ret.put("times", dates);
			ret.put("colNames", cols);
			ret.put("values", values);
			
			
			//System.out.println("times : "+dates);
			//System.out.println("colNames :  "+cols);
			
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			log.debug("DataAbnormalService.getMainDataAbnormal Error",e);

		}
		return null;
	}
	
	
	public List<Map<String, Object>> getwholeDayDataAbnormalsByDataset(String datasetID,Date time){
		List<Map<String, Object>> retVal=new ArrayList<Map<String,Object>>();
		try{
			Calendar calendar = Calendar.getInstance();
			if(datasetID==null||time==null)
				return null;
			calendar.setTime(time);
			Date start = CharliDate.getStartTimeOfCurrentDay(time);
			Date end = CharliDate.getStartTimeOfDaysApartCurrentDay(start, 1);
			
			
			List<DataAbnormal> list=dataAbnormalDao.getDataAbnormals(datasetID, start, end);
			
			for(DataAbnormal da:list){
				retVal.add(DataAbnormalUtil.createTableNodeByDataAbnormal(da));
			}
			return retVal;
			
		}catch (Exception e) {
			e.printStackTrace();
			log.debug("DataAbnormalService.getwholeDayDataAbnormalsByDataset Error",e);
		}
		return retVal;
	}
	//ListRange分页
	public ListRange getDataAbnormalBeanStoress(Map map) throws ParseException{
		ListRange range=new ListRange();
		List list=new ArrayList();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int start = Integer.parseInt(map.get("start").toString());
		int limit = Integer.parseInt(map.get("limit").toString());
		String id=(String) map.get("idss");
		String ss=(String) map.get("startTime");
		String se=(String) map.get("endTime");
		System.out.println("开始时间："+ss);
		System.out.println("结束时间："+se);
		Date startTime=null;
		Date endTime=null;
		startTime=sdf.parse(ss);
		endTime=sdf.parse(se);
		
		int totalSize=this.getCountDataAbnormalBeanByParams(id, startTime, endTime);
		list=this.getDataAbnormalBeanByParams8(id, startTime, endTime,start,limit);
		if(list==null){
			range.setData(null);
			range.setTotalSize(0);
		}else{
			range.setData(list.toArray());
			range.setTotalSize(totalSize);
		}
		return range;
	}
	//ListRange分页
	public ListRange getDataAbnormalBeanStoress2(Map map) throws ParseException{
		ListRange range=new ListRange();
		List<DataAbnormalBean> list=new ArrayList();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int start = Integer.parseInt(map.get("start").toString());
		int limit = Integer.parseInt(map.get("limit").toString());
		String type=map.get("type").toString();
		String id=(String) map.get("idss");
		String ss=(String) map.get("startTime");
		String se=(String) map.get("endTime");
		System.out.println("开始时间："+ss);
		System.out.println("结束时间："+se);
		Date startTime=null;
		Date endTime=null;
		startTime=sdf.parse(ss);
		endTime=sdf.parse(se);
		
		int totalSize=this.getCountDataAbnormalBeanByParams(id, startTime, endTime);
		list=this.getDataAbnormalBeanByParams(id, startTime, endTime);
		int c=0;
		for(int i=list.size()-1;i>=0;i--){
			if(!list.get(i).getAbnoarmalType().equals(type)&&!type.equals("ALL")){
				list.remove(list.get(i));
				c++;
			}
		}
		System.out.println("过滤了"+c+"条");
		if(list==null){
			range.setData(null);
			range.setTotalSize(0);
		}else{
			range.setData(list.toArray());
			range.setTotalSize(totalSize-c);
		}
		return range;
	}
	public List<DataAbnormalBean> getDataAbnormalBeanStoress3(String datasetID, String start, String end,String type) throws ParseException{
		List<DataAbnormalBean> list=new ArrayList();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startTime=null;
		Date endTime=null;
		startTime=sdf.parse(start);
		endTime=sdf.parse(end);
		list=this.getDataAbnormalBeanByParams(datasetID, startTime, endTime);
		int c=0;
		if(list==null){
			list=list;
		}else{
			for(int i=list.size()-1;i>=0;i--){
				if(!list.get(i).getAbnoarmalType().equals(type)&&!type.equals("ALL")){
					list.remove(list.get(i));
					c++;
				}
			}
		}
		System.out.println("过滤了"+c+"条");
		return list;
	}
	public int getCountDataAbnormalBeanByParams(String datasetID, Date start, Date end) {
		try{
			return dataAbnormalDao.getCountDataAbnormalBeanByParams(datasetID, start, end);
		} 
		catch (Exception e) {
			log.error("DataAbnormalService getCountDataAbnormalBeanByParams Error",e);
			e.printStackTrace();
		}
		return 0;
	}
	
	public List<DataAbnormalBean> getDataAbnormalBeanByParams8(String datasetID, Date start, Date end, int startRow, int endRow){
		endRow=endRow+startRow;
		return this.getDataAbnormalBeanByParams(datasetID, start, end, startRow, endRow);
	}
	
	public List<DataAbnormalBean> getDataAbnormalBeanByParams(String datasetID, Date start, Date end, int startRow, int endRow) {
		try{
			return dataAbnormalDao.getDataAbnormalBeanByParams(datasetID, start, end, startRow, endRow);
		} 
		catch (Exception e) {
			log.error("DataAbnormalService getDataAbnormalBeanByParams Error",e);
			e.printStackTrace();
		}
		return null;
	}
	
	public List<DataAbnormalBean> getDataAbnormalBeanByParams(String datasetID, Date start, Date end) {
		try{
			return dataAbnormalDao.getDataAbnormalBeanByParams(datasetID, start, end);
		} 
		catch (Exception e) {
			log.error("DataAbnormalService getDataAbnormalBeanByParams Error",e);
			e.printStackTrace();
		}
		return null;
	}

	public boolean removeDataAbnormal(String dataSetId, String kpiId, String neId, Date timeStamp) {
		try{
			dataAbnormalDao.removeDataAbnormal(dataSetId, kpiId, neId, timeStamp);
			return true;
		} 
		catch (Exception e) {
			log.error("DataAbnormalService removeDataAbnormal Error",e);
			e.printStackTrace();
		}
		
		return false;
	}

	public boolean removeIntegerAbnormal(String dataSetId, Date timeStamp) {
		try{
			dataAbnormalDao.removeIntegerAbnormal(dataSetId, timeStamp);
			return true;
		} 
		catch (Exception e) {
			log.error("DataAbnormalService removeIntegerAbnormal Error",e);
			e.printStackTrace();
		}
		
		return false;
	}

	public boolean removeIntegerAbnormalByDataSetTimeStampExp(List<String> dataSetTimeStamp) {
		if(dataSetTimeStamp==null||dataSetTimeStamp.isEmpty())
			return true;
		
		try{
			dataAbnormalDao.removeIntegerAbnormalByDataSetTimeStampExp(dataSetTimeStamp);
			return true;
		} 
		catch (Exception e) {
			log.error("DataAbnormalService removeIntegerAbnormalByDataSetTimeStampExp Error",e);
			e.printStackTrace();
		}
		
		return false;
	}
	public boolean removeIntegerAbnormalsWB(String dataSetId, List timeStamps){
		boolean flag=false;
		if(timeStamps!=null){
			List<Date> timeList=new ArrayList();
			for(int i=0;i<timeStamps.size();i++){
				//Date date=formatter.parse(dataTimes.get(i).toString());
				Date date=new Date(Long.parseLong(timeStamps.get(i).toString()));
				timeList.add(date);
			}
			flag=this.removeIntegerAbnormals(dataSetId, timeList);
		}else if(timeStamps==null){
			return flag;
		}
		return flag;
	}
	public boolean removeIntegerAbnormals(String dataSetId, List<Date> timeStamps) {
		if(timeStamps==null||timeStamps.isEmpty())
			return true;
		
		try{
			dataAbnormalDao.removeIntegerAbnormals(dataSetId, timeStamps);
			return true;
		} 
		catch (Exception e) {
			log.error("DataAbnormalService removeIntegerAbnormals Error",e);
			e.printStackTrace();
		}
		
		return false;
	}

}

